package com.ugoodtech.mdcc.client.controller;

import com.ugoodtech.mdcc.client.dto.JsonResponse;
import com.ugoodtech.mdcc.client.service.ServiceReportRecorderService;
import com.ugoodtech.mdcc.core.domain.User;
import com.ugoodtech.mdcc.core.repository.UserRepository;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.security.Principal;

@Api(description = "处理记录相关接口")
@RestController
@RequestMapping("/reportRecorder")
public class ServiceReportRecorderController {

    @Autowired
    private ServiceReportRecorderService serviceReportRecorderService;

    @Autowired
    private UserRepository userRepository;

    @ApiOperation("获取处理记录")
    @RequestMapping(value = "getRecorder", method = RequestMethod.GET)
    public JsonResponse getRecorder(Principal principal,
                                    @ApiParam("报告ID") @RequestParam(value = "reportId") Long reportId) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        return serviceReportRecorderService.getRecorder(reportId, clientUser.getId());
    }

    @ApiOperation("管理员获取跟进报告的部门列表")
    @RequestMapping(value = "managerGetRecorder", method = RequestMethod.GET)
    public JsonResponse managerGetRecorder(Principal principal,
                                           @ApiParam("报告ID") @RequestParam(value = "reportId") Long reportId) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        return serviceReportRecorderService.managerGetRecorder(reportId);
    }

    @ApiOperation("获取报告处理记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "getReportRecoder", method = RequestMethod.GET)
    public JsonResponse getReportRecoder(Principal principal,
                                         @ApiParam("报告ID") @RequestParam(value = "reportId") Long reportId,
                                         @ApiParam("部门ID") @RequestParam(value = "deparmentId", required = false) Long deparmentId,
                                         Pageable pageable) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        return serviceReportRecorderService.getReportRecoder(deparmentId, reportId, pageable);
    }

    @ApiOperation("获取报告历史记录")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = false, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页条数", required = false, paramType = "query", dataType = "int"),
    })
    @RequestMapping(value = "getHistoricalReportRecoder", method = RequestMethod.GET)
    public JsonResponse getHistoricalReportRecoder(Principal principal,
                                                   @ApiParam("报告ID") @RequestParam(value = "reportId") Long reportId,
                                                   Pageable pageable) {
        User clientUser = userRepository.findByUsername(principal.getName());
        if (clientUser == null) {
            return JsonResponse.errorResponseWithError("抱歉", "未找到此用户");
        }
        return serviceReportRecorderService.getHistoricalReportRecoder(reportId, pageable);
    }


}
